<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>proto2_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_proto2_proto2_test.js.html">proto2_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.proto2Test&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.proto2Test&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.proto2&#39;);
<a name="line20"></a>goog.require(&#39;proto2.TestAllTypes&#39;);
<a name="line21"></a>
<a name="line22"></a>function testAssertEquals() {
<a name="line23"></a>  var assertProto2Equals = goog.testing.proto2.assertEquals;
<a name="line24"></a>  assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes);
<a name="line25"></a>  assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes, &#39;oops&#39;);
<a name="line26"></a>
<a name="line27"></a>  var ex = assertThrows(goog.partial(assertProto2Equals,
<a name="line28"></a>      new proto2.TestAllTypes, new proto2.TestAllTypes.NestedMessage));
<a name="line29"></a>  assertEquals(
<a name="line30"></a>      &#39;Message type mismatch: TestAllTypes != TestAllTypes.NestedMessage&#39;,
<a name="line31"></a>      ex.message);
<a name="line32"></a>
<a name="line33"></a>  var message = new proto2.TestAllTypes;
<a name="line34"></a>  message.setOptionalInt32(1);
<a name="line35"></a>  ex = assertThrows(goog.partial(assertProto2Equals,
<a name="line36"></a>      new proto2.TestAllTypes, message));
<a name="line37"></a>  assertEquals(&#39;optional_int32 should not be present&#39;, ex.message);
<a name="line38"></a>
<a name="line39"></a>  ex = assertThrows(goog.partial(assertProto2Equals,
<a name="line40"></a>      new proto2.TestAllTypes, message, &#39;oops&#39;));
<a name="line41"></a>  assertEquals(&#39;oops\noptional_int32 should not be present&#39;, ex.message);
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function testFindDifferences_EmptyMessages() {
<a name="line45"></a>  assertEquals(&#39;&#39;, goog.testing.proto2.findDifferences_(
<a name="line46"></a>      new proto2.TestAllTypes, new proto2.TestAllTypes, &#39;&#39;));
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function testFindDifferences_FieldNotPresent() {
<a name="line50"></a>  var message = new proto2.TestAllTypes;
<a name="line51"></a>  message.setOptionalInt32(0);
<a name="line52"></a>  var empty = new proto2.TestAllTypes;
<a name="line53"></a>  assertEquals(&#39;optional_int32 should not be present&#39;,
<a name="line54"></a>      goog.testing.proto2.findDifferences_(empty, message, &#39;&#39;));
<a name="line55"></a>  assertEquals(&#39;optional_int32 should be present&#39;,
<a name="line56"></a>      goog.testing.proto2.findDifferences_(message, empty, &#39;&#39;));
<a name="line57"></a>  assertEquals(&#39;path/optional_int32 should be present&#39;,
<a name="line58"></a>      goog.testing.proto2.findDifferences_(message, empty, &#39;path&#39;));
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testFindDifferences_IntFieldDiffers() {
<a name="line62"></a>  var message1 = new proto2.TestAllTypes;
<a name="line63"></a>  message1.setOptionalInt32(1);
<a name="line64"></a>  var message2 = new proto2.TestAllTypes;
<a name="line65"></a>  message2.setOptionalInt32(2);
<a name="line66"></a>  assertEquals(&#39;optional_int32 should be 1, but was 2&#39;,
<a name="line67"></a>      goog.testing.proto2.findDifferences_(message1, message2, &#39;&#39;));
<a name="line68"></a>}
<a name="line69"></a>
<a name="line70"></a>function testFindDifferences_NestedIntFieldDiffers() {
<a name="line71"></a>  var message1 = new proto2.TestAllTypes;
<a name="line72"></a>  var nested1 = new proto2.TestAllTypes.NestedMessage();
<a name="line73"></a>  nested1.setB(1);
<a name="line74"></a>  message1.setOptionalNestedMessage(nested1);
<a name="line75"></a>  var message2 = new proto2.TestAllTypes;
<a name="line76"></a>  var nested2 = new proto2.TestAllTypes.NestedMessage();
<a name="line77"></a>  nested2.setB(2);
<a name="line78"></a>  message2.setOptionalNestedMessage(nested2);
<a name="line79"></a>  assertEquals(&#39;optional_nested_message/b should be 1, but was 2&#39;,
<a name="line80"></a>      goog.testing.proto2.findDifferences_(message1, message2, &#39;&#39;));
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>function testFindDifferences_RepeatedFieldLengthDiffers() {
<a name="line84"></a>  var message1 = new proto2.TestAllTypes;
<a name="line85"></a>  message1.addRepeatedInt32(1);
<a name="line86"></a>  var message2 = new proto2.TestAllTypes;
<a name="line87"></a>  message2.addRepeatedInt32(1);
<a name="line88"></a>  message2.addRepeatedInt32(2);
<a name="line89"></a>  assertEquals(&#39;repeated_int32 should have 1 items, but has 2&#39;,
<a name="line90"></a>      goog.testing.proto2.findDifferences_(message1, message2, &#39;&#39;));
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testFindDifferences_RepeatedFieldItemDiffers() {
<a name="line94"></a>  var message1 = new proto2.TestAllTypes;
<a name="line95"></a>  message1.addRepeatedInt32(1);
<a name="line96"></a>  var message2 = new proto2.TestAllTypes;
<a name="line97"></a>  message2.addRepeatedInt32(2);
<a name="line98"></a>  assertEquals(&#39;repeated_int32[0] should be 1, but was 2&#39;,
<a name="line99"></a>      goog.testing.proto2.findDifferences_(message1, message2, &#39;&#39;));
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>function testFindDifferences_RepeatedNestedMessageDiffers() {
<a name="line103"></a>  var message1 = new proto2.TestAllTypes;
<a name="line104"></a>  var nested1 = new proto2.TestAllTypes.NestedMessage();
<a name="line105"></a>  nested1.setB(1);
<a name="line106"></a>  message1.addRepeatedNestedMessage(nested1);
<a name="line107"></a>  var message2 = new proto2.TestAllTypes;
<a name="line108"></a>  var nested2 = new proto2.TestAllTypes.NestedMessage();
<a name="line109"></a>  nested2.setB(2);
<a name="line110"></a>  message2.addRepeatedNestedMessage(nested2);
<a name="line111"></a>  assertEquals(&#39;repeated_nested_message[0]/b should be 1, but was 2&#39;,
<a name="line112"></a>      goog.testing.proto2.findDifferences_(message1, message2, &#39;&#39;));
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>function testFromObject() {
<a name="line116"></a>  var nested = new proto2.TestAllTypes.NestedMessage();
<a name="line117"></a>  nested.setB(1);
<a name="line118"></a>  var message = new proto2.TestAllTypes;
<a name="line119"></a>  message.addRepeatedNestedMessage(nested);
<a name="line120"></a>  message.setOptionalInt32(2);
<a name="line121"></a>  // Successfully deserializes simple as well as message fields.
<a name="line122"></a>  assertObjectEquals(
<a name="line123"></a>      message,
<a name="line124"></a>      goog.testing.proto2.fromObject(proto2.TestAllTypes, {
<a name="line125"></a>        &#39;optional_int32&#39;: 2,
<a name="line126"></a>        &#39;repeated_nested_message&#39;: [{&#39;b&#39;: 1}]
<a name="line127"></a>      }));
<a name="line128"></a>  // Fails if the field name is not recognized.
<a name="line129"></a>  assertThrows(function() {
<a name="line130"></a>    goog.testing.proto2.fromObject(proto2.TestAllTypes, {&#39;unknown&#39;: 1});
<a name="line131"></a>  });
<a name="line132"></a>  // Fails if the value type is wrong in the JSON object.
<a name="line133"></a>  assertThrows(function() {
<a name="line134"></a>    goog.testing.proto2.fromObject(proto2.TestAllTypes,
<a name="line135"></a>        {&#39;optional_int32&#39;: &#39;1&#39;});
<a name="line136"></a>  });
<a name="line137"></a>}
</pre>


</body>
</html>
